import axios from 'axios'
import {
  MessageBox,
  Message,
  Loading
} from 'element-ui'
import store from '@/store'
import {
  getToken,
  removeToken
} from '@/utils/auth'
// import de from 'element-ui/src/locale/lang/de'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 30000, // request timeout,
  loading:()=>Loading.service({background:'rgba(0,0,0,0)'})
})

service.defaults.headers.post['Content-Type'] = 'application/json'

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      // config.headers['Content-Type'] = 'application/json'
      config.headers['token'] = getToken()
    }
    if(config.url)config.url +='?t='+new Date().getTime()
    //console.log("service",config)
    config.loading()
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    config.loading().close()
     //Loading.service().close()
    return Promise.reject(error)
  }
)
// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data

    if (!res.code) {
      // Message.error('缺少响应码')
      return res
    } else {
      //console.log("service",response)
      res.loading = false
      response.config.loading().close()
      //Loading.service().close()
      switch (res.code) {
        case '1':
        case 20000:
        case '-1':
          return res
        case 50008:
        case 50012:
        case 50014:
        case '000-1002-400':

        case 400:
          MessageBox.confirm('登录已过期，请重新登录', '提示', {
            confirmButtonText: '重新登录',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            store.dispatch('user/resetToken').then(() => {
              location.reload()
            })
          })
          break

        case 500:
          Message({
            message: '网络错误，请稍后再试',
            type: 'error',
            duration: 5 * 1000
          })
          return Promise.reject(new Error(res.message || 'Error'))
        case '000-1002-400':
          Message({
            message: res.msg,
            type: 'error',
            duration: 5 * 1000
          })

          removeToken()
          return Promise.reject(new Error(res.message || 'Error'))
        default:
          Message({
            message: res.msg || 'Error',
            type: 'error',
            duration: 5 * 1000
          })
          return Promise.reject({
            error: res.message || 'Error',
            loading: false
          })
      }
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    //Loading.service().close()
    response.config.loading().close()
    return Promise.reject(error)
  }
)

export default service
